<?php
// +----------------------------------------------------------------------
// | 存酒
// +----------------------------------------------------------------------
// | 首页方法控制器  创建:2018/8/18
// +----------------------------------------------------------------------
// | Author: wqb <609693168@qq.com>
// +----------------------------------------------------------------------
namespace app\index\controller;
use app\admin\model\OrderList;
use app\admin\model\WechatConfig;
use think\Controller;
use think\facade\Session;

require dirname(dirname(dirname(__DIR__))).'/vendor/Wxpay/WxPay.Api.php';
require dirname(dirname(dirname(__DIR__))).'/vendor/Wxpay/WxPay.JsApiPay.php';

class WXpay extends Controller
{
    public function mackupPay(){

        $order=OrderList::where('id',input('get.id'))->find();

        $wechatRes=WechatConfig::where('wx_id',1)->find();
        header("Content-Type:text/html;charset=utf-8");
        define('WX_APPID',$wechatRes['appid']);
        define('WX_MCHID',$wechatRes['mch_id']);
        define('WX_KEY',$wechatRes['paykey']);
        define('WX_APPSECRET',$wechatRes['appsecret']);

        $tal_fee=$order['pay_money']*100;
        $this->assign('tal_fee',$order['pay_money']);

        $tools = new \JsApiPay();

        //①、获取用户openid
        if(Session::get('openid')==""){
            if(empty(input('get.code'))){
                $openId = $tools->GetOpenid();
            }else{
                $openId = $tools->GetOpenid(input('get.code'));
            }
            Session::set('openid',$openId);
        }else{
            $openId =Session::get('openid');
        }


        //②、统一下单
        $input = new \WxPayUnifiedOrder();
        $input->SetBody("延长驾校收款");
        $input->SetAttach("延长驾校收款");
        $input->SetOut_trade_no($order['order_no']);//商户订单号

        $input->SetTotal_fee($tal_fee);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("延长驾校收款");
        $input->SetNotify_url("http://ycjx.taiyannet.com/index/Notify/mackupNotify");
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        $infos = new \WxPayApi();
        $order = $infos->unifiedOrder($input);
        $jsApiParameters = $tools->GetJsApiParameters($order);
        $this->assign('jsApiParameters',$jsApiParameters);
        return $this->fetch();
    }

    public function traincarPay(){
        $order=OrderList::where('id',input('get.id'))->find();

        $wechatRes=WechatConfig::where('wx_id',1)->find();
        header("Content-Type:text/html;charset=utf-8");
        define('WX_APPID',$wechatRes['appid']);
        define('WX_MCHID',$wechatRes['mch_id']);
        define('WX_KEY',$wechatRes['paykey']);
        define('WX_APPSECRET',$wechatRes['appsecret']);

        $tal_fee=$order['pay_money']*100;
        $this->assign('tal_fee',$order['pay_money']);
        $tools = new \JsApiPay();

        //①、获取用户openid
        if(Session::get('openid')==""){
            if(empty(input('get.code'))){
                $openId = $tools->GetOpenid();
            }else{
                $openId = $tools->GetOpenid(input('get.code'));
            }
            Session::set('openid',$openId);
        }else{
            $openId =Session::get('openid');
        }
        $openId =Session::get('openid');

        //②、统一下单
        $input = new \WxPayUnifiedOrder();
        $input->SetBody("延长驾校收款");
        $input->SetAttach("延长驾校收款");
        $input->SetOut_trade_no($order['order_no']);//商户订单号

        $input->SetTotal_fee($tal_fee);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("延长驾校收款");
        $input->SetNotify_url("http://ycjx.taiyannet.com/index/Notify/mackupNotify");
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        $infos = new \WxPayApi();
        $order = $infos->unifiedOrder($input);
        $jsApiParameters = $tools->GetJsApiParameters($order);
        $this->assign('jsApiParameters',$jsApiParameters);
        return $this->fetch();
    }


    public function goodsPay(){

        $order=OrderList::where('id',input('get.id'))->find();

        $wechatRes=WechatConfig::where('wx_id',1)->find();
        header("Content-Type:text/html;charset=utf-8");
        define('WX_APPID',$wechatRes['appid']);
        define('WX_MCHID',$wechatRes['mch_id']);
        define('WX_KEY',$wechatRes['paykey']);
        define('WX_APPSECRET',$wechatRes['appsecret']);

        $tal_fee=$order['pay_money']*100;
        $this->assign('tal_fee',$order['pay_money']);

        $tools = new \JsApiPay();

        //①、获取用户openid
        if(Session::get('openid')==""){
            if(empty(input('get.code'))){
                $openId = $tools->GetOpenid();
            }else{
                $openId = $tools->GetOpenid(input('get.code'));
            }
            Session::set('openid',$openId);
        }else{
            $openId =Session::get('openid');
        }


        //②、统一下单
        $input = new \WxPayUnifiedOrder();
        $input->SetBody("延长驾校收款");
        $input->SetAttach("延长驾校收款");
        $input->SetOut_trade_no($order['order_no']);//商户订单号

        $input->SetTotal_fee($tal_fee);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("延长驾校收款");
        $input->SetNotify_url("http://ycjx.taiyannet.com/index/Notify/mackupNotify");
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        $infos = new \WxPayApi();
        $order = $infos->unifiedOrder($input);
        $jsApiParameters = $tools->GetJsApiParameters($order);
        $this->assign('jsApiParameters',$jsApiParameters);
        $this->assign('type',(int)input('get.type'));
        $this->assign('order_id',(int)input('get.id'));
        return $this->fetch();
    }

}
